package com.superzqb.asus.superzqbUtil;


import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.view.ViewGroup;

import com.squareup.picasso.Transformation;

/**
 * Created by asus on 2017/1/26.
 * 一个自定义按图片小边缩放类
 */

public class BitmapTransformation implements Transformation {
    private ViewGroup.LayoutParams v;
    public BitmapTransformation(ViewGroup.LayoutParams i) {
        this.v=i;
    }
    @Override
    public Bitmap transform(Bitmap bitmap) {
        int w=bitmap.getWidth();
        int h = bitmap.getHeight();
        Matrix matrix = new Matrix();
        float scale = (float) v.width / w;
        float scale2 = (float) v.height / h;
        /*scale = scale < scale2 ? scale : scale2;//取小边为基准*/
        matrix.postScale(scale, scale);
        Bitmap bmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
        if ( !bitmap.equals(bmp) && !bitmap.isRecycled())
        {
            bitmap.recycle();
        }
        return bmp;// Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
    }

    @Override
    public String key() {
        return "scale()";
    }
}
